/**
 * BookUploadDAO.java
 * Creates a data access object (DAO) for an ebook data transfer object (DTO).
 * @author Sejung
 */
package com.example.myproject;

import java.util.List;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.util.DAOBase;

public class BookUploadDAO extends DAOBase{

    static {
        ObjectifyService.register(BookUploadDTO.class);
    }

    /**
     * Saves a BookUploadDTO to the blobstore.
     * @param dto The ebook object to be saved.
     */
    public BookUploadDTO save(BookUploadDTO dto) {
        ofy().put(dto);
        return dto;
    }
 
    /**
     * Retrieves BookUploadDTOs from the blobstore with matching IDs.
     * @param id Supplied search parameter.
     * @return Returns a key map of BookUploadDTOs matching id.
     */	
    public BookUploadDTO findById(Long id) {
    	if(id <= 1L)return null;
        Key<BookUploadDTO> key = new Key<BookUploadDTO>(BookUploadDTO.class, id);
        return ofy().get(key);
    }

    /**
     * Retrieves a list of all BookUploadDTOs in the blobstore.
     * @return List containing all BookUploadDTOs in the blobstore.
     */    	
	public List<BookUploadDTO> list() 
	{		
		List<BookUploadDTO> alist = ofy().query(BookUploadDTO.class).list();
		return alist;
	} 
    	
    /**
     * Deletes a BookUploadDTO from the blobstore, specified by id.
     * @param id Supplied id of the BookUploadDTO to be deleted.
     */
	public void delete(Long id) 
	{	
		if(id <= 1L)return;
		BookUploadDTO book = ofy().query(BookUploadDTO.class).filter("id ", id).get();
		ofy().delete(book);
	}
	
    /**
     * Deletes an BookUploadDTO from the blobstore, specified by parentId.
     * @param parentId Supplied parentId of the BookUploadDTO to be deleted.
     */
	public void deleteByParentId(Long parentId) 
	{	
		if(parentId <= 1L)return;
		BookUploadDTO book = ofy().query(BookUploadDTO.class).filter("parentId ", parentId).get();
		ofy().delete(book);
	}
	
}